package cn.cmkj.auction.app;

import android.app.Activity;
import android.app.Application;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.tencent.smtt.sdk.QbSdk;

import org.xutils.DbManager;
import org.xutils.db.table.TableEntity;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import cn.cmkj.auction.BuildConfig;
import cn.cmkj.auction.R;
import cn.cmkj.auction.data.http.GoodsCate;
import cn.cmkj.auction.utils.SharedPreferencesUtils;

/**
 * Created by cunguoyao on 2017/7/29.
 */

public class BaseApplication extends Application {

    private static String TAG = BaseApplication.class.getName();
    private static BaseApplication mInstance;
    private DbManager dbManager;
    /*Activity堆*/
    private Stack<Activity> activityStack = new Stack<Activity>();
    public DbManager getDbManager() {
        return dbManager;
    }

    private List<String> unSetStateBarColorPage;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        initUnSetStatusBarColorPage();
        x.Ext.init(this);
        x.Ext.setDebug(BuildConfig.DEBUG); // 是否输出debug日志, 开启debug会影响性能.
        initXdb();
        initImageLoader();
        initX5();
        initSharedPreferencesData();
    }

    public static synchronized BaseApplication getInstance() {
        return mInstance;
    }

    private void initUnSetStatusBarColorPage() {
        unSetStateBarColorPage = new ArrayList<>();
        unSetStateBarColorPage.add(getPackageName() + ".ui.SplashActivity");
        unSetStateBarColorPage.add(getPackageName() + ".ui.ImagePagerActivity");
    }

    public List<String> getUnSetStateBarColorPage() {
        return unSetStateBarColorPage;
    }

    private void initXdb() {
        /**
         * 初始化DaoConfig配置
         */
        DbManager.DaoConfig daoConfig = new DbManager.DaoConfig()
                //设置数据库名，默认xutils.db
                .setDbName("auction.db")
                //设置数据库路径，默认存储在app的私有目录
                //.setDbDir(new File("/mnt/sdcard/"))
                //设置数据库的版本号
                .setDbVersion(1)
                //设置数据库打开的监听
                .setDbOpenListener(new DbManager.DbOpenListener() {
                    @Override
                    public void onDbOpened(DbManager db) {
                        //开启数据库支持多线程操作，提升性能，对写入加速提升巨大
                        db.getDatabase().enableWriteAheadLogging();
                    }
                })
                //设置数据库更新的监听
                .setDbUpgradeListener(new DbManager.DbUpgradeListener() {
                    @Override
                    public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
                    }
                })
                //设置表创建的监听
                .setTableCreateListener(new DbManager.TableCreateListener() {
                    @Override
                    public void onTableCreated(DbManager db, TableEntity<?> table){
                    }
                });
        //设置是否允许事务，默认true
        //.setAllowTransaction(true)
        dbManager = x.getDb(daoConfig);
    }

    private void  initImageLoader() {
        DisplayImageOptions defaultOptions = new DisplayImageOptions
                .Builder()
                .showImageForEmptyUri(R.drawable.empty_photo)
                .showImageOnFail(R.drawable.empty_photo)
                .cacheInMemory(true)
                .cacheOnDisc(true)
                .build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration
                .Builder(getApplicationContext())
                .defaultDisplayImageOptions(defaultOptions)
                .discCacheSize(50 * 1024 * 1024)//
                .discCacheFileCount(100)//缓存一百张图片
                .writeDebugLogs()
                .build();
        ImageLoader.getInstance().init(config);
    }

    private void initX5() {
        //初始化X5内核
        QbSdk.initX5Environment(this, new QbSdk.PreInitCallback() {
            @Override
            public void onCoreInitFinished() {
                //x5内核初始化完成回调接口，此接口回调并表示已经加载起来了x5，有可能特殊情况下x5内核加载失败，切换到系统内核。

            }

            @Override
            public void onViewInitFinished(boolean b) {
                //x5內核初始化完成的回调，为true表示x5内核加载成功，否则表示x5内核加载失败，会自动切换到系统内核。
            }
        });
    }

    private void initSharedPreferencesData() {
        List<GoodsCate> cateList = new ArrayList<>();
        GoodsCate cate1 = new GoodsCate();
        cate1.cat_id = 23;
        cate1.cat_name = "所有分类";
        GoodsCate cate2 = new GoodsCate();
        cate2.cat_id = 24;
        cate2.cat_name = "国画";
        GoodsCate cate3 = new GoodsCate();
        cate3.cat_id = 25;
        cate3.cat_name = "油画";
        cateList.add(cate1);cateList.add(cate2);cateList.add(cate3);
        SharedPreferencesUtils.getInstance(this, "Art").putListData("goods_cate", cateList);
    }

    public void addActivity(final Activity curAT) {
        if (null == activityStack) {
            activityStack = new Stack<Activity>();
        }
        activityStack.add(curAT);
    }

    public void removeActivity(final Activity curAT) {
        if (null == activityStack) {
            activityStack = new Stack<Activity>();
        }
        activityStack.remove(curAT);
    }

    //获取最后一个Activity
    public Activity currentActivity() {
        Activity activity = activityStack.lastElement();
        return activity;
    }

    //关闭所有Activity
    public void clearActivity() {
        for (int i = 0, size = activityStack.size(); i < size; i++) {
            if (null != activityStack.get(i)) {
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }

}
